/*$(function(){
	$.ajax({
		url:"/crud/fglist",
		dataType:"json",
		data:"currentPage=1",
		type:"post",
		"success":function(data){
			var obj = $.parseJSON(data);
			alert(obj);
			var item; 
			$.each(data,function(i,result){
			//var re = result.list;
			item = "<tr><td><input type='checkbox'/></td><td>"+result['count']+"</td><td>"+result['pageMax']+"</td><td>"+result['currentPage']+"</td><td>操作</td><td>操作</td><td>操作</td><td>操作</td><td>操作</td><td>操作</td><td>操作</td></tr>"; 
			$("#goodtable").append(item); 
			}); 
		}
	});
	
});*/
/*$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);*/

//<td>"+goods.rnum+"</td>
$(function(){
	//点击页码更新list
	$(".oppage").each(function(){
		$(this).click(function(){
			
		});
	});
	//增加产品
	$(".addgoods").each(function(){
		$(this).click(function(){
			
		});
	});
	//上一页
	$(".backward").each(function(){
		$(this).click(function(){
			
		});
	});
	//下一页
	$(".foreward").each(function(){
		$(this).click(function(){
			
		});
	});
	$.getJSON("/crud/fglist?currentPage=1",function(data){
		var operate ="<div class='am-btn-group am-btn-group-xs'>"
         +"<button class='am-btn am-btn-default am-btn-xs am-text-secondary edit'><span class='am-icon-pencil-square-o'></span> 编辑</button>"
         +"<button class='am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only delete'><span class='am-icon-trash-o'></span> 删除</button>"
         +"</div>";
		//"+goods.fileid+"'
		var item;
		$.each(data.page.list,function(i,goods){
			//var gd = goods.gdate.format("yyyy-MM-dd hh:mm:ss.S");
			//var tt=new Date(parseInt(goods.gdate)).toLocaleString().substr(0,16);
			var tt=new Date(parseInt(goods.gdate)).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
			var v = "http://localhost/curd/"+goods.fileid;
			item = "<tr><td><input type='checkbox'/></td><td><img style='width:30px;height:30px' src='"+v+"'></td><td>"+goods.id+"</td><td>"+goods.gname+"</td><td>"+goods.gdespriction+"</td><td>"+goods.gprice+"</td><td>"+goods.gnum+"</td><td>"+tt+"</td><td>"+operate+"</td></tr>"; 
			$("#goodlist").append(item); 
		});
		$("#count").html("共"+data.page.count+"条记录"+"/共"+data.page.pageCount+"页");
		$("#fenye").html("<ul class='am-pagination'>" 
                  +"<li class='am-active'><a href='javascript:void(0);' id='currrentpage' name='currrentpage' class='oppage'>"+data.page.currentPage+"</a></li>"
                  +"</ul>");
		if(data.page.currentPage>0&&data.page.currentPage<=data.page.pageCount){
			for(var i=1;i<3&&i<=(data.page.pageCount-data.page.currentPage);i++){
				$("#fenye ul").append("<li><a href='javascript:void(0);' class='oppage'>"+(data.page.currentPage+i)+"</a></li>");
			}
			for(var i=1;i<3&&i<=(data.page.currentPage-1);i++){
				$("#fenye ul").prepend("<li><a href='javascript:void(0);' class='oppage'>"+(data.page.currentPage-i)+"</a></li>");
			}
			if(data.page.currentPage==1&&data.page.pageCount!=1){
				$("#fenye ul").prepend("<li class='am-disabled'><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}else if(data.page.currentPage==data.page.pageCount&&data.page.currentPage!=1){
				$("#fenye ul").prepend("<li><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li class='am-disabled'><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}else if(data.page.currentPage==data.page.pageCount&&data.page.currentPage==1){
				$("#fenye ul").prepend("<li class='am-disabled'><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li class='am-disabled'><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}else{
				$("#fenye ul").prepend("<li><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}
		}
	});
	$(".optable").on("click",".delete",function(){
	
		var goodsid = $(this).parent().parent().parent().children().first().next().next().html();
		//alert(goodsid);
		$(this).parent().parent().parent().remove();
		$.ajax({
			url:"/delgoods",
			type:"post",
			data:"goodsid="+goodsid,
			dataType:"text",
			"success":function(result){
				alert(result);
			}
		});
	});
	$(".optable").on("click",".edit",function(){
		var $aa=$(this).parent().parent().parent().children().first().next().next().next();
		//alert($("#editgoods").css("display"));
		if($("#editgoods").css("display")=="none"){
			$("#editgoods").css("display","block");
		}else{
			$("#editgoods").css("display","none");
		}
	
		
		$("#editgoods input[name='id']").val($aa.prev().html());
		$("#editgoods input[name='gname']").val($aa.html());
		$("#editgoods input[name='gdespriction']").val($aa.next().html());
		$("#editgoods input[name='gprice']").val($aa.next().next().html());
		$("#editgoods input[name='gnum']").val($aa.next().next().next().html());
		
	});
	//点击页码翻页
	$(".am-fr").on("click",".oppage",function(){
		$("#goodlist").find("tr").remove(); 
		var current = $(this).html();
		operatePage(current);
	});
	//点击上一页
	$(".am-fr").on("click",".backward",function(){
		$("#goodlist").find("tr").remove(); 
		var current = $(".am-fr .oppage[name='currrentpage']").html();		
		//alert(current);
		operatePage(parseInt(current)-parseInt(1));
	});
	//下一页分页
	$(".am-fr").on("click",".foreward",function(){
		$("#goodlist").find("tr").remove(); 
		var current = $(".am-fr .oppage[name='currrentpage']").html();
		//alert(current);
		operatePage(parseInt(current)+parseInt(1));
	});
	//前端按钮增加按钮
	$(".prebutton").on("click",".addgoods",function(){
		$('#my-prompt').modal({
		      relatedTarget: this,
		      onConfirm: function(e) {
		        var jsongoods = eval('('+e.data+')');
		        $.ajax({
		        	url:"/addgoods",
		        	dataType:"json",
		        	data:jsongoods,
		        	type:"post",
		        	"success":function(result){
		        		alert(result);
		        	}
		        });
		        
		      },
		      onCancel: function(e) {
		        //alert('不想说!');
		      }
		    });
	});
});
//添加商品
function addGoods(){
	
}
function operatePage(pageid){
	$.getJSON("/crud/fglist?currentPage="+pageid,function(data){
		var operate ="<div class='am-btn-group am-btn-group-xs'>"
         +"<button class='am-btn am-btn-default am-btn-xs am-text-secondary edit'><span class='am-icon-pencil-square-o'></span> 编辑</button>"
         +"<button class='am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only delete'><span class='am-icon-trash-o'></span> 删除</button>"
         +"</div>";
         
		var item;
		$.each(data.page.list,function(i,goods){
			//var gd = goods.gdate.format("yyyy-MM-dd hh:mm:ss.S");
			//alert(gd);
			var v = "http://localhost/curd/"+goods.fileid;
			item = "<tr><td><input type='checkbox'/></td><td><img style='width:30px;height:30px' src='"+v+"'></td><td>"+goods.id+"</td><td>"+goods.gname+"</td><td>"+goods.gdespriction+"</td><td>"+goods.gprice+"</td><td>"+goods.gnum+"</td><td>"+goods.gdate+"</td><td>"+operate+"</td></tr>"; 
			$("#goodlist").append(item); 
		});
		$("#count").html("共"+data.page.count+"条记录"+"/共"+data.page.pageCount+"页");
		$("#fenye").html("<ul class='am-pagination'>" 
                  +"<li class='am-active'><a href='javascript:void(0);' name='currrentpage' class='oppage'>"+data.page.currentPage+"</a></li>"
                  +"</ul>");
		if(data.page.currentPage>0&&data.page.currentPage<=data.page.pageCount){
			for(var i=1;i<3&&i<=(data.page.pageCount-data.page.currentPage);i++){
				$("#fenye ul").append("<li><a href='javascript:void(0);' class='oppage'>"+(data.page.currentPage+i)+"</a></li>");
			}
			for(var i=1;i<3&&i<=(data.page.currentPage-1);i++){
				$("#fenye ul").prepend("<li><a href='javascript:void(0);' class='oppage'>"+(data.page.currentPage-i)+"</a></li>");
			}
			if(data.page.currentPage==1&&data.page.pageCount!=1){
				$("#fenye ul").prepend("<li class='am-disabled'><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}else if(data.page.currentPage==data.page.pageCount&&data.page.currentPage!=1){
				$("#fenye ul").prepend("<li><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li class='am-disabled'><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}else if(data.page.currentPage==data.page.pageCount&&data.page.currentPage==1){
				$("#fenye ul").prepend("<li class='am-disabled'><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li class='am-disabled'><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}else{
				$("#fenye ul").prepend("<li><a href='javascript:void(0);' class='backward'>←</a></li>");
				$("#fenye ul").append("<li><a href='javascript:void(0);' class='foreward'>→</a></li>");
			}
		}
	});
};